﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Aandelendelen.Menu
{
	public class MenuCollection : Dictionary<string, MainMenu>
	{
		public MenuCollection()
		{
			this.ReadMenus();
		}

		private void ReadMenus()
		{
			string path = HttpContext.Current.Server.MapPath("/App_Data/TopMenu.xml");

			XmlDocument doc = new XmlDocument();

			doc.Load(path);

			XmlNodeList nodes = doc.SelectNodes("/aandelendelen/menu");

			if (nodes != null)
			{
				foreach (XmlNode node in nodes)
				{
					string type = node.Attributes["type"].Value;
					string direction = node.Attributes["direction"].Value;
					string css = null;
					string submenuCss = null;
					string separatorCss = null;

					MenuDirection menuDirection = (direction == "vertical" ? MenuDirection.Vertical : MenuDirection.Horizontal);

					XmlAttribute attr = node.Attributes["css"];
					if (attr != null)
						css = attr.Value;

					attr = node.Attributes["submenucss"];
					if (attr != null)
						submenuCss = attr.Value;

					attr = node.Attributes["separatorcss"];
					if (attr != null)
						separatorCss = attr.Value;

					MainMenu mainMenu = new MainMenu(type, menuDirection, css, submenuCss, separatorCss);

					mainMenu.Populate(node);

					this[type] = mainMenu;
				}
			}

			doc.RemoveAll();
		}

	}
}